<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */
 
class desktop_finder_tag{
    var $column_control = '编辑';
    function column_control($row){
       return '<a target="dialog::{title:\''.app::get('desktop')->_('链接编辑').'\', width:600, height:550}" href="index.php?app=tags&ctl=admin_tags&act=tag_edit&type='.$_GET['type'].'&finder_id='.$_GET['_finder']['finder_id'].'&_finder[finder_id]='.$_GET['_finder']['finder_id'].'&_finder_name='.$_GET['_finder']['finder_id'].'&p[0]='.$row['tag_id'].'">'.app::get('desktop')->_('编辑').'</a>';
    }

    /**
     * 标签列表的查看下拉
     * @param $row
     */
    var $detail_basic = '首页商品';
    function  detail_basic($tag_id){
        $app = app::get('tags');
        $render = $app->render();
        $goodsTagModel = app::get('b2c')->model('goods_tags');
        $goodsModel = app::get('b2c')->model('goods');
        $list = $goodsTagModel->getList('*', array('tags_id'=>$tag_id));
        array_walk($list, function (&$val) use ($goodsModel) {
            $goodsInfo = $goodsModel->getRow('*', array('goods_id'=>$val['goods_id']));
            $val['goods_name'] = $goodsInfo['name'];
        });
        $render->pagedata['goodsTagList'] = $list;
        $render->pagedata['tag_id'] = $tag_id;
        return $render->fetch('detail.html');
    }
    
    var $detail_goods = '标签商品';
    function  detail_goods($tag_id)
    {
        //批量删除
        if($_POST['act_mode'] == 'batchDeleteGoods') {
            $this->batchDeleteGoods();
        }
        
        $page_size = 10;
        $page = intval($_GET['detail_goods_page']);
        if($page == 0 ) $page = 1;
        $offset = ($page - 1) * $page_size;
        if(isset($_GET['tag_id'])){
            $tag_id = $_GET['tag_id'];
        }

        $app = app::get('tags');
        $render = $app->render();
        $tag_rel = app::get('desktop')->model('tag_rel');
        $goodsModel = app::get('b2c')->model('goods');
        $list = $tag_rel->getList('*', array('tag_id'=>$tag_id), $offset, $page_size);
        $count = $tag_rel->count(array('tag_id'=>$tag_id));
        array_walk($list, function (&$val) use ($goodsModel) {
            $goodsInfo = $goodsModel->getRow('*', array('goods_id'=>$val['rel_id']));
            $val['goods_name'] = $goodsInfo['name'];
            $val['goods_bn'] = $goodsInfo['bn'];
            $val['goods_id'] = $goodsInfo['goods_id'];
        });
        $render->pagedata['total_goods_num'] = $count;
        $render->pagedata['finder_id'] = $_GET['finder_id'];
        $render->pagedata['goodsTagList'] = $list;
        $render->pagedata['tag_id'] = $tag_id;
        $nPage = $_GET['detail_order'] ? $_GET['detail_order'] : 1;
        $render->pagedata['pager']= $this->pagination($nPage,$count, $page,$page_size,$tag_id);
        return $render->fetch('tag_goods.html');
    }
    
    public function pagination($current,$count,$page,$page_size,$tag_id){ //本控制器公共分页函数
        $app = app::get('b2c');
        $render = $app->render();
        $ui = new base_component_ui($app);
        $link = 'index.php?action=detail&app='.$_GET['app'].'&ctl='.$_GET['ctl'].'&act='.$_GET['act'].'&finder_id='.$_GET['finder_id'].'&finderview='.$_GET['finderview'].'&tag_id='.$tag_id.'&detail_goods_page=%d';
        $this->pagedata['pager'] = $ui->pager(array(
            'current'=>$page,
            'total'=>ceil($count/$page_size),
            'link' =>$link,
            'page' => $page
        ));
        return $this->pagedata['pager'];
    }
    
    public function batchDeleteGoods()
    {
        $item_ids = $_POST['item_ids'];
        
        if( ! $item_ids){
            return false;
        }
        
        $tag_rel = app::get('desktop')->model('tag_rel');
        foreach($item_ids as $v){
            list($tag_id, $goods_id) = explode('###', $v);
            $tag_id = floatval($tag_id);
            $goods_id = floatval($goods_id);
            $tag_rel->delete(array('tag_id'=>$tag_id,'rel_id'=>$goods_id));
        }
    }
    
    
}
